using EnoApi.Domain;
using EnoApi.Entity;
using Microsoft.EntityFrameworkCore;

namespace EnoApi.Repositories;

public class EfRepository<T> : IReporsitory<T> where T : BlogEntity
{
    private readonly BlogsDbContext _db;
    private DbSet<T> _tb;

    public EfRepository(BlogsDbContext blogsDb)
    {
        _db = blogsDb;
        _tb = blogsDb.Set<T>();
    }
    public T AddBlog(T entity)
    {
        var obj = _tb.Add(entity).Entity;
        _db.SaveChanges();
        return obj;
    }

    public T DelBlog(T entity)
    {
        _tb.Remove(entity);
        _db.SaveChanges();
        return entity;
    }

    public List<T> GetAll()
    {
        var list = _tb.ToList();
        return list;
    }

    public T? GetById(int id)
    {
        return _tb.Find(id);
    }

    public T UpdBlog(T entity)
    {
        _tb.Update(entity);
        _db.SaveChanges();
        return entity;
    }

}

